package com.yuyou.tas.admin.api;

import com.yuyou.tas.admin.service.ICommJobService;
import com.yuyou.tas.service.api.admin.entity.CommJob;
import com.yuyou.tas.service.api.admin.service.CommJobAPI;
import com.yuyou.tas.service.api.lesson.entity.LessonResource;
import com.yuyou.tas.service.api.lesson.service.LessonResourceAPI;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.enums.admin.CommJobEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;





@RestController
public class CommJobAPIImpl implements CommJobAPI{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());  
	
	@Autowired
	private ICommJobService commJobService;
	
	@Autowired
	private LessonResourceAPI lessonResourceAPI;
	
	@Override
	public ResBoolSimpleInfo<String> updateLoadedJob(@RequestParam("allotTempId")Long allotTempId) {
		commJobService.updateLoadedJob(allotTempId);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public List<CommJob> findLoadedJob(@RequestParam("allotTempId") Long allotTempId) {
		return commJobService.findLoadedJob(allotTempId);
	}

	@Override
	public ResBoolSimpleInfo<String> addResourceJob(
			@RequestParam("resourceId") String resourceId,
			@RequestParam("type")Integer type, 
			@RequestParam("tag")String tag) {
		
		LessonResource lessonResource = lessonResourceAPI.selectByResourceId(resourceId);
		if(lessonResource!=null && lessonResource.getId() != null){
			 commJobService.addJob(lessonResource.getId(), type, tag);	
		}

		return ResBoolSimpleInfo.getSuccessInfo();
		
	}

	@Override
	public ResBoolSimpleInfo<String> updateJobStatus(
			@RequestParam("fId") Long fId, 
			@RequestParam("type") Integer type,
			@RequestParam("tag") String tag, 
			@RequestParam("status") Integer status) {
		commJobService.updateJobStatus(fId, type, tag, status);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public void updateJob(Long id , Integer status , String remark) {
		if(id != null){
				CommJob job;

				if(CommJobEnums.status.fail.getValue() == status){
					job = commJobService.selectByPrimaryKey(id);
					if(job != null && job.getNeedRetry() == 1 && job.getMaxTime() != job.getHasTime()){
						job.setHasTime(job.getHasTime() +1);
						job.setStatus(CommJobEnums.status.init.getValue());

						//计算下次运行时间（增加间隔时间）
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(new Date());
						calendar.add(Calendar.MINUTE,CommJobEnums.interval.getMaxByTimes(job.getHasTime()));
						job.setCreateTime(calendar.getTime());

					}else {
						job.setUpdateTime(new Date());
						job.setStatus(status);
						job.setRemark(remark);
					}
				}else{
					job = new CommJob();
					job.setId(id);
					job.setUpdateTime(new Date());
					job.setStatus(status);
					job.setRemark(remark);
				}


				commJobService.updateByPrimaryKeySelective(job);
		}
	}

	@Override
	public void insertJobs(@RequestBody List<CommJob> list) {
		commJobService.insertJobs(list);
	}

	@Override
	public void alarmCommJob() {
		commJobService.alarmCommJob();
	}

	@Override
	public ResBoolSimpleInfo<String> addJob(
			@RequestParam("fId") Long fId, 
			@RequestParam("type") Integer type, 
			@RequestParam("tag") String tag) {
		commJobService.addJob(fId, type, tag);	
		return ResBoolSimpleInfo.getSuccessInfo();
	}


}
